package com.boho.ecp.util;

import java.io.File;
import java.io.FileFilter;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.boho.framework.connection.ConnectionHelper;
import cn.boho.framework.po.CQuery;
import cn.boho.framework.po.CQueryFactory;
import cn.boho.framework.po.CQueryFactoryTool;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.POUtils;
import cn.boho.framework.po.SArrayList;
import cn.boho.framework.po.UpdatePOList;
import cn.boho.framework.utils.InitTestTool;

import com.boho.ecp.po.TProductDetailPO;
import com.google.gson.reflect.TypeToken;


public class Test {
	
	private static String fileName = "D:/workspace/jllt-ecp/WebRoot/WEB-INF/config.xml";
	private static String logName = "D:/workspace/jllt-ecp/WebRoot/WEB-INF/log.xml";
	private static Connection con = null;
	
	public static void rename(){
		File file = new File("D://product");
		File[] files = file.listFiles();
		for (File f : files) {
			String fileName = f.getName().split("\\.")[0];
			System.out.println("D://rename/" + fileName + ".jpg");
			f.renameTo(new File("D://rename/" + fileName + ".jpg"));
		}
		System.out.println(files.length);
	}
	

	public static void main(String[] args) {
//		rename();
//		syncProductDetail(); //商品详细信息
//		updateProductPicCount(); // 更新图片数量
		fillModelProductId(); // 回填板块商品ID
	}
	
	/**
	 * 同步商品详细信息
	 */
	public static void syncProductDetail(){
		try {
			InitTestTool.initConfigFile(fileName, logName);
			CQueryFactoryTool.DB_TYPE = CQueryFactory.CQUERY_TYPE_MYSQL_5;
			con = ConnectionHelper.getConnection();
			con.setAutoCommit(false);
			String sql = "select * from T_PRODUCT order by PRODUCT_ID";
			CQuery query = CQueryFactoryTool.createFactory().createCQuery();
			query.setCommand(sql);
			
			Date date = new Date();
			SArrayList poList = new SArrayList();
			List list = query.getDynaBeanMapList("T_PRODUCT", con);
			for (Object object : list) {
				DynaBeanMap dbm = (DynaBeanMap)object;
				Integer productId = (Integer)dbm.get("PRODUCT_ID");
				TProductDetailPO po = new TProductDetailPO();
				po.setProductId(productId);
				po.setCreateDate(date);
				poList.add(po);
			}
			POUtils.insertPoList(con, poList, false);
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			ConnectionHelper.finalRelease(con);
		}
		
	}
	
	/**
	 * 更新商品图片数量
	 */
	public static void updateProductPicCount(){
		try{
			InitTestTool.initConfigFile(fileName, logName);
			CQueryFactoryTool.DB_TYPE = CQueryFactory.CQUERY_TYPE_MYSQL_5;
			con = ConnectionHelper.getConnection();
			con.setAutoCommit(false);
			String sql = "select * from T_PRODUCT order by PRODUCT_ID";
			CQuery query = CQueryFactoryTool.createFactory().createCQuery();
			query.setCommand(sql);
			
			File filePath = new File("D:\\product");
			
			UpdatePOList updateList = new UpdatePOList();
			List list = query.getDynaBeanMapList("T_PRODUCT", con);
			PreparedStatement ps = con.prepareStatement("update t_product_detail set DETAIL_COUNT = ?, DESC_COUNT = ? where PRODUCT_ID = ?");
			for (Object object : list) {
				DynaBeanMap dbm = (DynaBeanMap)object;
				final String productNo = (String)dbm.get("PRODUCT_NO");
				Integer productId = (Integer)dbm.get("PRODUCT_ID");
				FileFilter filter = new FileFilter(){
					public boolean accept(File pathname) {
						return pathname.getName().startsWith(productNo);
					}
				};
				File[] files = filePath.listFiles(filter);
				int detailCount = 0;
				for (File file : files) {
					if(file.getName().startsWith(productNo + "_N_")){
						detailCount++;
					}
				}
				
				int descCount = 0;
				if(files.length != 0){
					descCount = files.length - detailCount - 1;
				}
				
				System.out.println("productNo:" + productNo + " files.length:" + files.length + " detailCount:" + detailCount + " descCount:" + descCount);
				
				ps.setInt(1, detailCount);
				ps.setInt(2, descCount);
				ps.setInt(3, productId);
				ps.addBatch();
			}
			ps.executeBatch();
			ps.close();
			con.commit();
		} catch(Exception e){
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally{
			ConnectionHelper.finalRelease(con);
		}
	}
	
	/**
	 * 填充模块ID
	 */
	public static void fillModelProductId(){
		try {
			InitTestTool.initConfigFile(fileName, logName);
			CQueryFactoryTool.DB_TYPE = CQueryFactory.CQUERY_TYPE_MYSQL_5;
			con = ConnectionHelper.getConnection();
			con.setAutoCommit(false);
			
			PreparedStatement ps = con.prepareStatement("update T_PLATE_PRODUCT set PLATE_PRODUCTS = ? where PLATE_CODE = ?");
			System.out.println("修改T_PLATE_PRODUCT");
			String sql = "select * from T_PLATE_PRODUCT";
			CQuery query = CQueryFactoryTool.createFactory().createCQuery();
			query.setCommand(sql);
			List list = query.getDynaBeanMapList("INDEX_MODEL", con);
			for (Object object : list) {
				DynaBeanMap dbm = (DynaBeanMap)object;
				String plateCode = (String)dbm.get("PLATE_CODE");
				List<IndexModel> indexList = JsonUtils.fromJson((String)dbm.get("PLATE_PRODUCTS"), new TypeToken<List<IndexModel>>(){}.getType()) ;
				List modelList = new ArrayList();
				for (IndexModel indexModel : indexList) {
					String no = indexModel.getProductNo();
					CQuery query1 = CQueryFactoryTool.createFactory().createCQuery();
					query1.setString(1, no);
					query1.setCommand("select PRODUCT_ID from T_PRODUCT where PRODUCT_NO = ?");
					DynaBeanMap m = query1.getDynaBeanMap("PRODUCT", con);
					System.out.println(no);
					Integer id = (Integer)m.get("PRODUCT_ID");
					indexModel.setProductId(id);
					modelList.add(indexModel);
				}
				String json = JsonUtils.toJSON(modelList);
				
				ps.setString(1, json);
				ps.setString(2, plateCode);
				ps.addBatch();
			}
			ps.executeBatch();
			
			System.out.println("修改T_OFFER_PRODUCT");
			ps = con.prepareStatement("update T_OFFER_PRODUCT set PRODUCT_ID = ? where PRODUCT_NO = ?");
			sql = "select * from T_OFFER_PRODUCT";
			query = CQueryFactoryTool.createFactory().createCQuery();
			query.setCommand(sql);
			list = query.getDynaBeanMapList("LIST", con);
			for (Object object : list) {
				DynaBeanMap dbm = (DynaBeanMap)object;
				CQuery query1 = CQueryFactoryTool.createFactory().createCQuery();
				String no = (String)dbm.get("PRODUCT_NO");
				query1.setString(1, no);
				query1.setCommand("select PRODUCT_ID from T_PRODUCT where PRODUCT_NO = ?");
				DynaBeanMap m = query1.getDynaBeanMap("PRODUCT", con);
				System.out.println(no);
				Integer id = (Integer)m.get("PRODUCT_ID");
				ps.setInt(1, id);
				ps.setString(2, no);
				ps.addBatch();
			}
			ps.executeBatch();
			
			System.out.println("修改T_QUICK_ORDER");
			ps = con.prepareStatement("update T_QUICK_ORDER set PRODUCT_ID = ? where PRODUCT_NO = ?");
			sql = "select * from T_QUICK_ORDER";
			query = CQueryFactoryTool.createFactory().createCQuery();
			query.setCommand(sql);
			list = query.getDynaBeanMapList("LIST", con);
			for (Object object : list) {
				DynaBeanMap dbm = (DynaBeanMap)object;
				CQuery query1 = CQueryFactoryTool.createFactory().createCQuery();
				String no = (String)dbm.get("PRODUCT_NO");
				query1.setString(1, no);
				query1.setCommand("select PRODUCT_ID from T_PRODUCT where PRODUCT_NO = ?");
				DynaBeanMap m = query1.getDynaBeanMap("PRODUCT", con);
				System.out.println(no);
				Integer id = (Integer)m.get("PRODUCT_ID");
				ps.setInt(1, id);
				ps.setString(2, no);
				ps.addBatch();
			}
			ps.executeBatch();
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			ConnectionHelper.finalRelease(con);
		}
		
	}
	
	//"productNo":"13020465","productName":"贝亲海棉奶瓶刷04039","marketPrice":49,"memberPrice":39.2
	class IndexModel{
		private Integer productId;
		private String productNo;
		private String productName;
		private BigDecimal marketPrice;
		private BigDecimal memberPrice;
		public Integer getProductId() {
			return productId;
		}
		public void setProductId(Integer productId) {
			this.productId = productId;
		}
		public String getProductNo() {
			return productNo;
		}
		public void setProductNo(String productNo) {
			this.productNo = productNo;
		}
		public String getProductName() {
			return productName;
		}
		public void setProductName(String productName) {
			this.productName = productName;
		}
		public BigDecimal getMarketPrice() {
			return marketPrice;
		}
		public void setMarketPrice(BigDecimal marketPrice) {
			this.marketPrice = marketPrice;
		}
		public BigDecimal getMemberPrice() {
			return memberPrice;
		}
		public void setMemberPrice(BigDecimal memberPrice) {
			this.memberPrice = memberPrice;
		}
	}
}
